前言

本文介绍使用eclipse+maven搭建Spring + SpringMvc + Hibernate项目,以登录为例:
  1. 创建maven项目
  2. 把maven项目变为动态网站
  3. 搭建spring+springmvc+Hibernate项目

1.创建maven项目

参考文章:https://blog.csdn.net/tiandixuanwuliang/article/details/79250504#t0

2.把maven项目变为动态网站

参考文章:https://blog.csdn.net/tiandixuanwuliang/article/details/79250504#t1

3.开始搭建spring + springmvc + Hibernate项目

3.1 配置maven依赖,在pom.xml里写入如下依赖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>club.hdqyf</groupId>
<artifactId>SSHdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.6.RELEASE</spring.version>
</properties>

<dependencies>
<!-- springframework 4 dependencies begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springframework 4 dependencies end -->

<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>

<!-- hibernate 配置 begin -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version>
</dependency>
<!-- hibernate 配置 end -->

<!-- mysql数据库的驱动包 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<!-- 引入jstl包 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>

<!-- 引入servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<!-- 引入数据库连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
3.2 编写实体类User以及与其对应的User.hbm.xml(数据库表的xml,与数据库表的字段对应)
  • 😍 User实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package club.hdqyf.entity;

/**
* @ClassName: User
* @Description:TODO(测试SSH的一个实体类)
* @author: Garen
* @date: 2018年8月15日 下午1:43:58
* @Copyright: 2018 www.hdqyf.club All rights reserved.
*/
public class User {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(Integer id, String userName, String password) {
super();
this.id = id;
this.userName = userName;
this.password = password;
}

public User() {
super();
}

@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password=" + password + "]";
}
}
  • 😍 User.hbm.xml
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="club.hdqyf.entity">
<class name="User" table="user">
<id name="id">
<generator class="native" />
</id>
<property name="userName" length="32" not-null="true" unique="true" />
<property name="password" length="64" not-null="true" />
</class>
</hibernate-mapping>
3.3 编写User的控制器UserController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package club.hdqyf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import club.hdqyf.entity.User;
import club.hdqyf.service.UserService;

/**
* @ClassName: UserController
* @Description:TODO(与User对象相关的Controller)
* @author: Garen
* @date: 2018年8月15日 下午1:47:01
* @Copyright: 2018 www.hdqyf.club All rights reserved.
*/
@Controller
public class UserController {
@Autowired
private UserService userService;

@RequestMapping("/login.action")
public String login(User user) {
// 查询到是否存在登录的这个用户
boolean status = userService.findUserByIdAndName(user.getUserName(), user.getPassword());

if (status) {
return "success";
} else {
return "login";
}
}
}
3.4 编写Service层相关接口和类
  • 😍 服务接口UserService
1
2
3
4
5
6
7
8
9
10
11
12
package club.hdqyf.service;

/**
* @ClassName: UserService
* @Description:TODO(User的服务层接口)
* @author: Garen
* @date: 2018年8月15日 下午1:50:20
* @Copyright: 2018 www.hdqyf.club All rights reserved.
*/
public interface UserService {
public boolean findUserByIdAndName(String userName, String password);
}
  • 😍 服务接口实现类UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package club.hdqyf.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import club.hdqyf.mapper.UserMapper;
import club.hdqyf.service.UserService;

/**
* @ClassName: UserServiceImpl
* @Description:TODO(User的服务接口实现类)
* @author: Garen
* @date: 2018年8月15日 下午1:50:43
* @Copyright: 2018 www.hdqyf.club All rights reserved.
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public boolean findUserByIdAndName(String userName, String password) {
boolean status = false;
if (userMapper.findUserByIdAndName(userName, password) != null) {
status = true;
}

return status;
}
}
3.5 编写Mapper(Dao)层相关接口和类
  • 😍 Dao接口UserMapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package club.hdqyf.mapper;

import club.hdqyf.entity.User;

/**
* @ClassName: UserDao
* @Description:TODO(User对象的数据操作接口)
* @author: Garen
* @date: 2018年8月15日 下午1:47:57
* @Copyright: 2018 www.hdqyf.club All rights reserved.
*/
public interface UserMapper {
public User findUserByIdAndName(String userName, String password);
}
  • 😍 Dao接口实现类UserMapperImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package club.hdqyf.mapper.imlp;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import club.hdqyf.entity.User;
import club.hdqyf.mapper.UserMapper;

/**
* @ClassName: UserMapperImpl
* @Description:TODO(Dao接口实现类UserMapperImpl)
* @author: Garen
* @date: 2018年8月15日 下午2:08:43
* @Copyright: 2018 www.hdqyf.club All rights reserved.
*/
@Repository
public class UserMapperImpl extends HibernateDaoSupport implements UserMapper {
// HibernateDaoSupport来操作数据库更加方便

// 用来注入sessionFactory(不注入会报错)
@Resource(name = "sessionFactory")
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}

@Override
public User findUserByIdAndName(String userName, String password) {
// 注意:以下是HQL语句
List<?> user = getHibernateTemplate().find("from User where userName=? and password=?", userName, password);

return user.size() > 0 ? (User) user.get(0) : null;
}
}
3.6 编写SpringMVC的配置文件和hibernate的配置文件
  • 😍 springMVC.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,
@DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等 -->
<mvc:annotation-driven />

<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="club.hdqyf"></context:component-scan>

<!-- 视图层配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置JSTL表达式-->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 配置前缀 -->
<property name="prefix" value="/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
  • 😍 applicationContext-dao.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="club.hdqyf" />

<!-- 引入数据源属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 数据源,BasicDataSource,commons-dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- Hibernate SesssionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 关联已经配置好的数据源 -->
<property name="dataSource" ref="dataSource"></property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- 数据库表有则改,无则添加 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<property name="mappingLocations" value="classpath:/club/hdqyf/entity/*.hbm.xml" />
</bean>

<!-- PlatTranscationManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
3.7 编写web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>maven-SSH</display-name>

<!-- 请求都交给DispatcherServlet处理 -->
<servlet>
<servlet-name>maven-SSH</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>maven-SSH</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- 增加中文乱码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>

<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

到这里我们的Spring + SpringMVC + Hibernate 项目就算搭建完成了!

4.项目下载地址

项目下载地址:https://github.com/GarenMorbid/Simple_Demos/tree/master/SSHdemo

ps:因作者能力有限,有错误的地方请见谅

  • 喜欢这篇文章的话可以用快捷键 Ctrl + D 来收藏本页
× 请我吃糖~
打赏二维码